package com.xiguanlezz.cn.common.remote.client;

import com.xiguanlezz.cn.api.ability.constant.AbilityKey;
import com.xiguanlezz.cn.api.ability.constant.AbilityStatus;
import com.xiguanlezz.cn.api.remote.Requester;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections.MapUtils;

import java.util.Map;

/**
 * @Author：习惯了种种
 * @CreateDate：2024/11/19 14:53
 * @Description：发送请求基类
 */
public abstract class Connection implements Requester {
    @Setter
    @Getter
    private String connectionId;
    @Setter
    @Getter
    private boolean abandon = false;

    protected RpcClient.ServerInfo serverInfo;

    @Setter
    @Getter
    protected Map<String, Boolean> abilityTable /* 连接支持的能力表 */;

    public Connection(RpcClient.ServerInfo serverInfo) {
        this.serverInfo = serverInfo;
    }

    public AbilityStatus getConnectionAbility(AbilityKey abilityKey) {
        if (abilityTable == null || !abilityTable.containsKey(abilityKey.getName())) {
            return AbilityStatus.UNKNOWN;
        }
        return abilityTable.get(abilityKey.getName()) ? AbilityStatus.SUPPORTED : AbilityStatus.NOT_SUPPORTED;
    }

    public boolean isAbilitiesSet() {
        return MapUtils.isNotEmpty(abilityTable);
    }

}